package com.myhost.model;

import java.io.UnsupportedEncodingException;

import android.annotation.SuppressLint;
import com.myhost.AppConstant;
import com.myhost.utils.Convert;
import com.myhost.utils.HostApplication;

@SuppressLint("ParserError")
public class UDPInstruction {

	public int cmd;
	public int id;
	public int destId;
	public String str;
	
	public UDPInstruction(byte temp[]) {
		byte byte1[] = new byte[4];
		byte byte2[] = new byte[4];
		byte byte3[] = new byte[4];
		System.arraycopy(temp, 0, byte1, 0, 4);
		System.arraycopy(temp, 4, byte2, 0, 4);
		System.arraycopy(temp, 8, byte3, 0, 4);
		cmd = Convert.Bytes2Int(byte1);
		id = Convert.Bytes2Int(byte2);
		destId = Convert.Bytes2Int(byte3);
		str = new String(temp, 12, AppConstant.UDP_INS_SIZE - 12).trim();
		// Log.i("ins", cmd + + pathSize + nameSize + fileSize);
	}
	
	public static byte[] getInsByte(int cmd, int destId, String str){
		byte[] temp = new byte[AppConstant.UDP_INS_SIZE];
		if(str != null){
			byte[] strByte = null;
			try {
				strByte = str.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			System.arraycopy(strByte, 0, temp, 12, strByte.length);
		}
		System.arraycopy(Convert.intToByteArrayForJava(cmd), 0, temp, 0, 4);
		System.arraycopy(Convert.intToByteArrayForJava(HostApplication.getId()), 0, temp, 4, 4);
		System.arraycopy(Convert.intToByteArrayForJava(destId), 0, temp, 8, 4);
		return temp;
	}

	@Override
	public String toString() {
		return "UDPInstruction [cmd=" + cmd + ", id=" + id + ", destId="
				+ destId + ", str=" + str + "]";
	}




	
	
}
